import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendEmail {

	public static void main(String[] args) {
		String to_1 = "18701321850@163.com";
		String to_2 = "773134958@qq.com";
		String[] tos = {to_1, to_2};
		String subject = "ERROR";
		String text = "测试邮件文本内容";
		String html = "您好，请<b><a href='https://www.baidu.com' target='_self'>点击链接</a></b>进行注册...";
		sendEmail(tos, subject, html, true);
	}
	
	/**
	 * 发送邮件	文本、html
	 * @param tos		接受者邮箱数组
	 * @param subject	邮件主题
	 * @param text		邮件正文内容
	 * @param html		是否使用html格式发送邮件
	 * @return
	 */
	public static Integer sendEmail(String [] tos, String subject, String text, Boolean html) {
		if (tos.length < 1) {
			return -2;
		}
		html = html == null ? true : html;
		// 发件人电子邮箱
//		String from = "773134958@qq.com";
		String from = ConfigUtil.getProperties("from");
		String name = "YourLifeAssistant";
		// 指定发送邮件的主机为 smtp.qq.com
//		String host = "smtp.qq.com"; // QQ 邮件服务器
		String host = ConfigUtil.getProperties("host");
//		 String host = "smtp.exmail.qq.com"; //QQ 邮件服务器

		// 获取系统属性
		Properties properties = System.getProperties();

		// 设置邮件服务器
		properties.setProperty("mail.smtp.host", host);
		properties.put("mail.smtp.auth", "true");
		// 下面四行是针对qq邮箱的ssl验证
		properties.put("mail.smtp.ssl.enable", "true");
		properties.put("mail.smtp.socketFactory.class", "javax.NET.ssl.SSLSocketFactory");
		properties.put("mail.smtp.port", "465");
		properties.put("mail.smtp.socketFactory.port", "465");
		// 获取默认session对象
		Session session = Session.getDefaultInstance(properties,
				new Email_Authenticator(ConfigUtil.getProperties("username"), ConfigUtil.getProperties("password")));
		// 设置为debug模式，打印发送过程信息
		session.setDebug(false);
		try {
			// 创建默认的 MimeMessage 对象
			MimeMessage message = new MimeMessage(session);
			message.setFrom(new InternetAddress(from, name));
			if (tos.length == 1) { // 单人发送
				message.addRecipient(Message.RecipientType.TO, new InternetAddress(tos[0]));
			} else { // 多人发送
				InternetAddress[] addTos = new InternetAddress[tos.length];
				for (int i = 0; i < tos.length; i++) {
					addTos[i] = new InternetAddress(tos[i]);
				}
				message.addRecipients(Message.RecipientType.TO, addTos);
			}
			message.setSubject(subject);
			message.setText(text);
			if (html) {
				message.setContent(text, "text/html;charset=utf8");
			}
			Transport.send(message);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}
}

class Email_Authenticator extends Authenticator {

	String userName = null;
	String password = null;

	public Email_Authenticator() {
	}

	public Email_Authenticator(String username, String password) {
		this.userName = username;
		this.password = password;
	}

	protected PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication(userName, password);
	}
}